//
//  MovieListViewController.m
//  newDouban
//
//  Created by dllo on 15/10/8.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieListViewController.h"
#import "MovieListTableViewCell.h"
#import "MovieDetailViewController.h"
#import "AllMovieDetailViewController.h"
#import "AppTools.h"
#import "Movie.h"
#import "MBProgressHUD.h"
#import "MovieDataSingTon.h"
#import "MJRefresh.h"
#import "Reachability.h"
@interface MovieListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) NSMutableArray *movieArray;
@property (nonatomic, retain) MBProgressHUD *hub;

@end

@implementation MovieListViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.tintColor = [UIColor orangeColor];
    
}


- (void)dealloc
{
    [_hub release];
    [_tableV release];
    [_movieArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBarController.tabBar.tintColor = [UIColor orangeColor];
    self.navigationItem.title = @"电影";
    
    
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 414, 736 - 64) style:UITableViewStyleGrouped];
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableV registerClass:[MovieListTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    
    [self.view addSubview:self.tableV];
    [_tableV release];
    [self openDB];
    //[self dropTable];
    
    self.movieArray = [NSMutableArray array];
    NSMutableArray *arrM = [self selectMovie];
    self.movieArray = [NSMutableArray arrayWithArray:arrM];
    [self.tableV reloadData];
    
    
    [self.tableV addHeaderWithCallback:^{
        
        NSLog(@"下拉");
        [self freash];
        
    }];
    [self.tableV headerBeginRefreshing];
    
}


- (void)UIAlerController
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"网路连接出错" preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alertC animated:YES completion:^{
        
    }];
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}

- (void)freash
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([r currentReachabilityStatus]) {
        case NotReachable:
            NSLog(@"没有网络连接");
            // 没有网络连接
            [self UIAlerController];
            [self.tableV headerEndRefreshing];
            
            break;
        case ReachableViaWWAN:
            // 使用3G网络
            NSLog(@"使用3G网络");
            
            break;
        case ReachableViaWiFi:
            // 使用WiFi网络
            NSLog(@"使用WiFi网络");
            
            self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            [self.hub show:YES];
            
            self.hub.labelText = @"正在加载中....";
            
            break;
    }
    
    
    
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    
    
    [AppTools getDataWithURL:urlStr block:^(id result) {
        
        [self dropTable];
        [self createTable];
        
        [self.movieArray removeAllObjects];
        NSMutableDictionary *dic = result;
        NSString *reason = [dic objectForKey:@"reason"];
        NSMutableArray *array = [dic objectForKey:@"result"];
        if ([reason isEqualToString:@"success"]) {
            for (NSDictionary *dicTemp in array) {
                Movie *movie = [[Movie alloc] init];
                [movie setValuesForKeysWithDictionary:dicTemp];
                [self.movieArray addObject:movie];
                [self insertMovie:movie];
                [movie release];
            }
            
            [self.tableV headerEndRefreshing];
            [self.tableV reloadData];
            [self.hub hide:YES];
            
            
        } else {
            NSLog(@"请求失败");
        }
        
    }];
}

//打开数据
- (void)openDB
{
    [[MovieDataSingTon shareMovieDataBase] openDB];
}

//创建表
- (void)createTable
{
    [[MovieDataSingTon shareMovieDataBase] createTable];
}
//插入数据
- (void)insertMovie:(Movie *)movie
{
    
    
    [[MovieDataSingTon shareMovieDataBase] insertMovie:movie];
}
//查询数据
- (NSMutableArray *)selectMovie
{
    NSMutableArray *arrM = [[MovieDataSingTon shareMovieDataBase] selectMovie];
    
    return arrM;
    
}
//删除表
- (void)dropTable
{
    [[MovieDataSingTon shareMovieDataBase] dropTable];
}













- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //选中时候有置灰效果,离开时消失选中效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AllMovieDetailViewController *allMovieDTVC = [[AllMovieDetailViewController alloc] init];
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    allMovieDTVC.movieID = movie.myMovieId;
    NSLog(@"movie.Myovied =  %@", movie.myMovieId);
    [self.navigationController pushViewController:allMovieDTVC animated:YES];
    [allMovieDTVC release];
    
}


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.movieArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    MovieListTableViewCell *cell = [[MovieListTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    
    static NSString *cellIndentifier = @"cell";
    MovieListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    
    cell.movie = movie;
    
    
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
